package com.aluvfy.jdbc;

import com.aluvfy.jdbc.utils.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*使用PreparedStatement完成模糊查询*/
public class JDBCTest13 {
    /*查询第二个字母为d的数据*/
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = DbUtils.getConnection();
            String sql = "SELECT name FROM t_user WHERE name LIKE ?";
            ps = conn.prepareStatement(sql);
            //给模糊查询传入一个字符串
            ps.setString(1, "_d%");
            rs = ps.executeQuery();
            if (rs.next()){
                System.out.println(rs.getString("name"));
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.close(conn, ps, rs);
        }
    }
}

